package nsu.edu.zsq.controller.student;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.Message;
import nsu.edu.zsq.bean.StuReward;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.student.StudentInfoService;
import nsu.edu.zsq.service.sys.MessageService;

/** 
* Description: 学生自身信息的controller类<br>
*/
@Controller
@RequestMapping("/student/info")
public class StudentInfoController {

	@Autowired
    private MessageService messageService;
    @Autowired
    private StudentInfoService studentInfoService;
    
  
    /**
     * 到达学生的个人信息页面
     * @param model
     * @return String student/info/student_info.html
     */
    @RequestMapping("/toStudent_info")
    public String toStudent_info(Model model) {
        Student student = RequestHolder.getCurrentStudent();
        model.addAttribute("student", student);
        return "student/info/student_info.html";
    }
    
    /**
     * 更新学生登录密码
     * @param password 密码
     * @return JsonData
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public JsonData updatePassword(@RequestParam("password")String password) {
        studentInfoService.updatePassword(password);
        return JsonData.success();
    }
    
    // =================================个人信息管理================================
    
    /**
     * 跳转到学生学籍信息页面，携带学生个人信息
     * @param model
     * @return String student/info/student_schoolInfo.html
     */
    @RequestMapping("/toStudent_schoolInfo")
    public String toStudent_schoolInfo(Model model) {
        Student student = studentInfoService.personalInfo();
        model.addAttribute("student", student);
        return "student/info/student_schoolInfo.html";
    }
    
    /**
     * 获取该学生的奖惩信息列表
     * @return JsonData
     */
    @RequestMapping("/listStuReward")
    @ResponseBody
    public JsonData listStuReward() {
        List<StuReward> listStuReward = studentInfoService.listStuReward();
        return JsonData.success(listStuReward);
    }
    
    /**
     * 跳转到查看奖惩信息页面
     * @param model
     * @return String student/info/student_reward_detail.html
     */
    @RequestMapping("/toLookStuReward")
    public String toLookStuReward(@RequestParam("rewardId")int rewardId, Model model) {
        StuReward stuReward = studentInfoService.getStuReward(rewardId);
        model.addAttribute("stuReward", stuReward);
        return "student/info/student_reward_detail.html";
    }
    
    
    
    
    /**
     * 跳转到欢迎页面，显示学校置顶消息
     * @param model 
     * @return String student/student_welcome.html
     */
    @RequestMapping("/toStudent_welcome")
    public String toStudent_welcome(Model model) {
        Message message = messageService.getTopMessage();
        model.addAttribute("message", message);
        model.addAttribute("student", RequestHolder.getCurrentStudent());
        return "student/student_welcome.html";
    }
    
    /**
     * 获取学校消息列表
     * @param PageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listMessage")
    @ResponseBody
    public JsonData listMessage(PageQuery pageQuery) {
        PageInfo<Message> listMessage = studentInfoService.listMessageWithPage(pageQuery);
        return JsonData.success(listMessage);
    }
    
    /**
     * 跳转学校消息详情页
     * @param model 
     * @return String student/info/student_schoolMsg_detail.html
     */
    @RequestMapping("/toMessageDetail")
    public String toMessageDetail(@RequestParam("id")int id, Model model) {
        Message message = messageService.getMessage(id);
        model.addAttribute("message", message);
        return "student/info/student_schoolMsg_detail.html";
    }
}
